package com.wxproject.msg.material;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.publicapi.AccessTokenApi;
import com.util.APIStart;
import com.util.ApiResult;
import com.util.HttpUtils;

/**
 * @author wangkai 素材维护
 */
public class MaterialApi {

	private static String uploadUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
	private static String getUrl = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
	private static String addnewsUrl = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
	private static String uploadimgUrl = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN";
	private static String addmaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE";
	private static String getmaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
	private static String delmaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=ACCESS_TOKEN";
	private static String updatenewsUrl = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=ACCESS_TOKEN";
	private static String batchgetmaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";

	/**
	 * 新增临时素材
	 * 
	 * @param type媒体文件类型
	 *            ，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb）
	 * @return
	 */
	public static ApiResult upload(String type) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(
					uploadUrl
							.replaceAll(
									"ACCESS_TOKEN",
									AccessTokenApi.getAccessTokenApi()
											.getAccessToken()).replaceAll(
									"TYPE", type), null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String media_id = jsonObject.getString("media_id");
			if (media_id != null) {
				return new ApiResult(APIStart.S00, "上传成功！", result);
			}
		}
		return new ApiResult(APIStart.S01, "上传失败！", result);
	}

	/**
	 * 获取临时素材
	 * 
	 * @param media_id
	 * @return
	 */
	public static ApiResult get(String media_id) {
		String result = null;
		try {
			result = HttpUtils.GETHTTPS(getUrl.replaceAll("ACCESS_TOKEN",
					AccessTokenApi.getAccessTokenApi().getAccessToken())
					.replaceAll("MEDIA_ID", media_id));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ApiResult(APIStart.S00, result);
	}

	/**
	 * 新增永久图文素材
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static ApiResult addNews(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(addnewsUrl.replaceAll("ACCESS_TOKEN",
					AccessTokenApi.getAccessTokenApi().getAccessToken()),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String media_id = jsonObject.getString("media_id");
			if (media_id != null) {
				return new ApiResult(APIStart.S00, "上传成功！", result);
			}
		}
		return new ApiResult(APIStart.S01, "上传失败！", result);
	}

	/**
	 * 上传图文消息内的图片获取URL 
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static ApiResult uploadimg(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(uploadimgUrl.replaceAll(
					"ACCESS_TOKEN", AccessTokenApi.getAccessTokenApi()
							.getAccessToken()), jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String url = jsonObject.getString("url");
			if (url != null) {
				return new ApiResult(APIStart.S00, "上传成功！", result);
			}
		}
		return new ApiResult(APIStart.S01, "上传失败！", result);
	}

	/**
	 * 新增其他类型永久素材
	 * 
	 * @param type媒体文件类型
	 *            ，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb）
	 * @param jsonStr
	 * @return
	 */
	public static ApiResult addMaterial(String type, String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(
					addmaterialUrl
							.replaceAll(
									"ACCESS_TOKEN",
									AccessTokenApi.getAccessTokenApi()
											.getAccessToken()).replaceAll(
									"TYPE", type), jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String media_id = jsonObject.getString("media_id");
			if (media_id != null) {
				return new ApiResult(APIStart.S00, "上传成功！", result);
			}
		}
		return new ApiResult(APIStart.S01, "上传失败！", result);
	}

	/**
	 * 获取永久素材
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static ApiResult getMaterial(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(getmaterialUrl.replaceAll(
					"ACCESS_TOKEN", AccessTokenApi.getAccessTokenApi()
							.getAccessToken()), jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String errcode = jsonObject.getString("errcode");
			if (errcode != null) {
				return new ApiResult(APIStart.S01, result);
			}
		}
		return new ApiResult(APIStart.S00, result);
	}

	/**
	 * 删除永久素材
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static ApiResult delMaterial(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(delmaterialUrl.replaceAll(
					"ACCESS_TOKEN", AccessTokenApi.getAccessTokenApi()
							.getAccessToken()), jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String errcode = jsonObject.getString("errcode");
			if (errcode != null && !errcode.trim().equals("0")) {
				return new ApiResult(APIStart.S01, result);
			}
		}
		return new ApiResult(APIStart.S00, result);
	}

	/**
	 * 修改永久图文素材
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static ApiResult updateNews(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(updatenewsUrl.replaceAll(
					"ACCESS_TOKEN", AccessTokenApi.getAccessTokenApi()
							.getAccessToken()), jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String errcode = jsonObject.getString("errcode");
			if (errcode != null && !errcode.trim().equals("0")) {
				return new ApiResult(APIStart.S01, result);
			}
		}
		return new ApiResult(APIStart.S00, result);
	}

	/**
	 * 获取素材列表
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static ApiResult batchgetMaterial(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(batchgetmaterialUrl.replaceAll(
					"ACCESS_TOKEN", AccessTokenApi.getAccessTokenApi()
							.getAccessToken()), jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			String errcode = jsonObject.getString("errcode");
			if (errcode != null && !errcode.trim().equals("0")) {
				return new ApiResult(APIStart.S01, result);
			}
		}
		return new ApiResult(APIStart.S00, result);
	}

}
